angular.module('mamApp').controller('adminUserManageController', ['$scope', '$state', '$uibModal', 'SystemManager', 'RoleManager', 'ImsErr',
    function ($scope, $state, $uibModal, SystemManager, RoleManager, ImsErr) {

        $scope.formData = {departmentId: "", roleId: "",firDepartmentId:""};

        RoleManager.roleListSimple().then(function (result) {
            $scope.roleList = [{roleId: "", roleName: "角色"}].concat(result.data.list);
        });


/*        SystemManager.departmentsList().then(function (result) {
            $scope.departments = [];
            $scope.departments = [{departmentId: "", departmentName: "一级部门"}].concat(result.data.departments)
        })
        $scope.departmentChange = function (departmentId) {
            $scope.secDepartments = [];
            SystemManager.departmentsList({firstId: departmentId}).then(function (result) {
                $scope.secDepartments = [{departmentId: "", departmentName: "二级部门"}].concat(result.data.departments)

                //console.log($scope.secDepartments)
            })
        }*/

        //一级部门初始
        SystemManager.departmentsList().then(function (result) {
            $scope.departments = [{departmentId: "", departmentName: "一级部门"}].concat(result.data.departments)
        });
        //二级部门初始
        $scope.secDepartments = [{departmentId: "", departmentName: "二级部门"}];

        //一级部门选择-->二级部门变化
        $scope.departmentChange = function (departmentId) {
            $scope.search.pageNo=1;
            $scope.formData.departmentId = "";
            $scope.secDepartments = [{departmentId: "", departmentName: "二级部门"}];
            SystemManager.departmentsList({firstId: departmentId}).then(function (result) {
                if (departmentId){
                    $scope.secDepartments = $scope.secDepartments.concat(result.data.departments);
                }
            })
        };

        //修改
        $scope.adminId = $state.params.adminId;
        if ($scope.adminId) {
            SystemManager.adminUserDetail($scope.adminId).then(function (result) {
                $scope.formData = result.data.userAdminVO;
                $scope.formData.firDepartmentId = parseInt($scope.formData.departmentId.toString().substring(0, 3));

                if ($scope.formData.firDepartmentId == $scope.formData.departmentId){
                    $scope.formData.departmentId = "";
                }

                SystemManager.departmentsList({firstId: $scope.formData.firDepartmentId}).then(function (result) {
                    if ($scope.formData.firDepartmentId){
                        $scope.secDepartments = $scope.secDepartments.concat(result.data.departments);
                    }
                });
            })
        }

        //提交
        $scope.submit = function () {
            $scope.toSave=angular.copy($scope.formData);
            delete $scope.toSave.firDepartmentId;

            if ($scope.adminId) {
                SystemManager.adminUserEdit($scope.toSave).then(function (result) {
                    if (result.data.c == "s") {
                        $state.go("main.adminUsers", {}, {reload: true});
                    } else {
                        ImsErr.open({message: result.data.i}).then(function (a) {
                        })
                    }
                })
            } else {
                SystemManager.adminUserAdd($scope.toSave).then(function (result) {
                    if (result.data.c == "s") {
                        $state.go("main.adminUsers", {}, {reload: true});
                    } else {
                        ImsErr.open({message: result.data.i}).then(function (a) {
                        })
                    }
                })
            }


        }


    }]);

